<?php

use App\Models\Auth\User;
use App\Models\Permission\Permission;
use App\Models\Permission\Role;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AssignSuperadminRoleForAdminToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $user = User::where([
                'username' => 'superadmin',
            ])->first();
            $role = Role::where([
                'name' => 'Super Role'
            ])->first();
    
            $user->roles()->sync([$role->id]);
            $role->permissions()->sync(Permission::pluck('id'));
            $user->accessTokens()->delete();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            //
        });
    }
}
